<?php

/**
 * This is the model class for table "pages".
 *
 * The followings are the available columns in table 'pages':
 * @property integer $id
 * @property string $name
 * @property string $text
 * @property string $title
 * @property string $keywords
 * @property string $descriptions
 * @property integer $status
 */
class Pages extends CActiveRecord
{

	const STATUS_NEW = 0;
	const STATUS_PUBLIC = 1;
	const STATUS_DELETE = 5;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Pages the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public static function get_array_status(){
		return array(
			self::STATUS_NEW => Yii::t('main', 'Не опубліковано'),
			self::STATUS_PUBLIC => Yii::t('main', 'Опубліковано'),
			self::STATUS_DELETE => Yii::t('main', 'Видалено'),
		);
	}

	public static function get_status_text($status=0){
		$array=self::get_array_status();

		if(!empty ($array[$status])){
			return $array[$status];
		}else {
			return FALSE;
		}
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'pages';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name,status', 'required'),
			array('status', 'numerical', 'integerOnly'=>true),
			array('name, title, keywords, descriptions', 'length', 'max'=>250),
			array('text', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Назва сторінки',
			'text' => 'Текст',
			'title' => 'SEO_Title',
			'keywords' => 'SEO_Keywords',
			'descriptions' => 'SEO_Descriptions',
			'status' => 'Статус',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Функція яка перевіряє чи можна видаляти сторінку
	 * ця перевірка поки дійсна для переіврки на головну.
	 */
	public static function check_delete($id){
		if(Yii::app()->user->checkAccess(Users::ROLE_ADMIN) && $id != 1){
			return TRUE;
		}else{
			return FALSE;
		}
	}
}